import {HTTP} from "../../utils/http.js";
import {ActModel} from "../../models/ActModel.js";
import {config} from "../../config.js";
var _ActModel = new ActModel();
var http = new HTTP();
var _self;
const app = getApp();
Page({
  data:{
    tabList:[
      {name:"全部",page:0},
      {name:"我参加的",page:1},
      {name:"我发起的",page:2},
     ],
    currentSwiperPage:0,
    currentTab:0,
    windowHeight:'',
    actAllList:[],
    participatedActList:[],
    myInitiateAct:[],
    defaultPageSize:2,
    signTarget:null,
    actQrShow: false,
    pageNum1:1,
    pageNum2:1,
    pageNum3:1,
    pagenation1: {
      pageNum: 1,  //页码
      pageSize: config.pageSize, //每页显示多少数据
      pages: 0,   //一共有几页
      total: 0,
      hasMore: true //是否有更多数据 
    },
    pagenation2: {
      pageNum: 1,  //页码
      pageSize: config.pageSize, //每页显示多少数据
      pages: 0,   //一共有几页
      total: 0,
      hasMore: true //是否有更多数据 
    },
    pagenation3: {
      pageNum: 1,  //页码
      pageSize: config.pageSize, //每页显示多少数据
      pages: 0,   //一共有几页
      total: 0,
      hasMore: true //是否有更多数据 
    }
  },
  _switchNav: function (e) {
    this.setData({
      currentSwiperPage: e.currentTarget.dataset.current,
      currentTab: e.currentTarget.dataset.current
    })
  },
  _bindSwiperChange: function (e) {
    this.setData({
      currentSwiperPage: e.detail.current,
      currentTab: e.detail.current
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _self = this;
    
    this.setData({
      windowHeight: app.globalData.windowHeight,
      currentType: options.id ? options.id : 0
    });
    _self.allActList();
    _self.participatedAct();
    _self.myInitiateAct();
    
  },
  allActList:function(){
    _ActModel.getAllActivis(_self.data.pagenation1,function(data){
      if (data.length > 0) {
        if (data[0].list.length < _self.data.pagenation1.pageSize) {
          _self.setData({
            ['pagenation1.hasMore']: false,
            isNoMoreData: true
          })
        }
        _self.setData({
          ['pagenation1.pageNum']: data[0].pageNum,
          ['pagenation1.pageSize']: data[0].pageSize,
          ['pagenation1.pages']: data[0].pages,
          ['pagenation1.total']: data[0].total,
          actAllList: _self.data.actAllList.concat(data[0].list)
        });
      }
    });
  },
  participatedAct:function(){
    _ActModel.getAllActivis(_self.data.pagenation2, function (data) {
      if (data.length > 0) {
        if (data[0].list.length < _self.data.pagenation2.pageSize) {
          _self.setData({
            ['pagenation2.hasMore']: false,
            isNoMoreData: true
          })
        }
        _self.setData({
          ['pagenation2.pageNum']: data[0].pageNum,
          ['pagenation2.pageSize']: data[0].pageSize,
          ['pagenation2.pages']: data[0].pages,
          ['pagenation2.total']: data[0].total,
          participatedActList: _self.data.participatedActList.concat(data[0].list)
        });
      }
    });
  },
  myInitiateAct:function(){
    _ActModel.myInitiateAct(_self.data.pagenation3, function (data) {
      if (data.length > 0) {
        if (data[0].list.length < _self.data.pagenation3.pageSize) {
          _self.setData({
            ['pagenation3.hasMore']: false,
            isNoMoreData: true
          })
        }
        _self.setData({
          ['pagenation3.pageNum']: data[0].pageNum,
          ['pagenation3.pageSize']: data[0].pageSize,
          ['pagenation3.pages']: data[0].pages,
          ['pagenation3.total']: data[0].total,
          myInitiateAct: _self.data.myInitiateAct.concat(data[0].list)
        });
      }
    });
  },
  getLastAct:function(){
    this.myInitiateAct();
  },
  onShow: function () {
    // _self.setData({
    //   actAllList: [],
    //   participatedActList: [],
    //   myInitiateAct: []
    // })
 
  },
  onShareAppMessage: function () {

  },
  swichNav: function (res) {
    if (this.data.currentType == res.detail.currentNum) return;
    this.setData({
      currentType: res.detail.currentNum
    })
  },
  bindChange:function(e){
    this.setData({
      currentType: e.detail.current
    })
  },
  enrollAct:function(event){
    let actid=event.detail.act_id
    wx.navigateTo({
      url: '/pages/activity/detail/detail?act_id=' + actid
    })
  },
  popQRCode: function (event){
  
    let actid = event.detail.act_id;
    let target= event.target.dataset.target;
    _self.setData({
      signTarget:target,
      actQrShow:true
    })
  },
  closeActQr:function(){
    _self.setData({
      actQrShow: false
    })
  },
  loadMoreAct:function(){
     if(_self.data.currentTab==0){
       let pagenation = _self.data.pagenation1;
       if (pagenation.hasMore) {
         let newPageNum = pagenation.pageNum + 1;
         let pages = pagenation.pages;
         if (newPageNum > pages) {
           _self.setData({
             ['pagenation1.hasMore']: false,
           })
         } else {
           _self.setData({
             ['pagenation1.pageNum']: newPageNum
           })
           _self.allActList();
         }

       } else {
         _self.setData({
           ['pagenation1.hasMore']: false
         })
       }
     }


    if (_self.data.currentTab == 1) {
      let pagenation = _self.data.pagenation2;
      if (pagenation.hasMore) {
        let newPageNum = pagenation.pageNum + 1;
        let pages = pagenation.pages;
        if (newPageNum > pages) {
          _self.setData({
            ['pagenation2.hasMore']: false,
          })
        } else {
          _self.setData({
            ['pagenation2.pageNum']: newPageNum
          })
          _self.participatedAct();
        }

      } else {
        _self.setData({
          ['pagenation2.hasMore']: false
        })
      }
    }


    if (_self.data.currentTab == 2) {
      let pagenation = _self.data.pagenation3;
      if (pagenation.hasMore) {
        let newPageNum = pagenation.pageNum + 1;
        let pages = pagenation.pages;
        if (newPageNum > pages) {
          _self.setData({
            ['pagenation3.hasMore']: false,
          })
        } else {
          _self.setData({
            ['pagenation3.pageNum']: newPageNum
          })
          _self.myInitiateAct();
        }

      } else {
        _self.setData({
          ['pagenation3.hasMore']: false
        })
      }
    }
  }
})